/*
 * Copyright (c) 2019. Mikes. All rights reserved.
 *
 * You can contact me with sending email to bWlrZXNfMjAxMkAxNjMuY29t for more information!
 */

package com.climber.android.commonres.widget

import android.content.Context
import android.util.AttributeSet
import android.view.View
import io.library.android.adapter.LQRAdapterForRecyclerView
import io.library.android.recyclerview.LQRRecyclerView

/**
 * Created by Mikes at 2019-11-15 19:59
 */
class LoadMoreLQRRecyclerView : LQRRecyclerView{
    constructor(context: Context) : super(context)
    constructor(context: Context, attrs : AttributeSet) : super(context, attrs)

    private val loadMoreFooterView : LoadMoreFooterView

    fun getLoadMoreFooterView() : LoadMoreFooterView = loadMoreFooterView

    fun initLoadingFooterView(adapter : LQRAdapterForRecyclerView<*>){
        adapter.addFooterView(getLoadMoreFooterView())
    }

    init {
        loadMoreFooterView = LoadMoreFooterView(context)

        setLoadingMoreEnabled(true)
    }

    fun loadMoreDoing(){
        loadMoreFooterView.changeLoadMoreState(LoadMoreState.doing)
        adapter?.run {
            smoothMoveToPosition(itemCount- 1)
        }
    }

    fun loadMoreDone(){
        loadMoreComplete()

        loadMoreFooterView.changeLoadMoreState(LoadMoreState.done)
    }

    fun noMoreData(){
        setLoadingMoreEnabled(false)

        loadMoreFooterView.changeLoadMoreState(LoadMoreState.noMore)
    }

    fun loadMoreError(retryClick : View.OnClickListener){
        loadMoreComplete()

        loadMoreFooterView.changeLoadMoreState(LoadMoreState.error)
        loadMoreFooterView.setReloadListener(retryClick)
    }
}